package com.sh.data.engine.web.webapi.controller.normalization;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.research.ThemeResponse;
import com.sh.data.engine.application.model.request.normalization.MoveThemeRequest;
import com.sh.data.engine.application.model.request.normalization.SaveThemeRequest;
import com.sh.data.engine.application.service.normalization.ThemeApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : mingxi.xpb
 * @date : 2022/1/6 18:06
 */
@Validated
@RestController
@RequestMapping("/theme")
@Tag(name = "规范设计-主题设计", description = "规范设计-主题设计API")
public class ThemeController {

    @Autowired
    private ThemeApplicationService themeApplicationService;

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZTSJ, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/saveTheme")
    @Operation(summary = "新建或编辑主题")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.主题域)
//    @Log(
//        business = Business.主题域,
//        operation = Operation.ADD_UPDATE)
    public RestResponseEntity<Boolean> saveTheme(@Valid @RequestBody SaveThemeRequest themeRequest) {
        var result = themeApplicationService.saveTheme(themeRequest);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZTSJ, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/deleteTheme")
    @Operation(summary = "删除主题")
    @LogExecution(operation = EngineOperation.DEL, business = Business.主题域)
//    @Log(
//        business = Business.主题域,
//        operation = Operation.DEL)
    public RestResponseEntity<Boolean> deleteTheme(@Parameter(description = "主题id") @RequestParam Long themeId) {
        var result = themeApplicationService.deleteTheme(themeId);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZTSJ, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/listThemes")
    @Operation(summary = "主题列表")
    public RestResponseEntity<PagerResponse<ThemeResponse>> listThemes(
        @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
        @Parameter(description = "当前页最后一条数据的id")
        @RequestParam(required = false) String lastThemeId,
        @Parameter(description = "父级id") @RequestParam Long pid) {
        var result = themeApplicationService.listThemes(pageNum, pageSize, lastThemeId, pid);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZTSJ, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/treeThemes")
    @Operation(summary = "主题树形结构")
    public RestResponseEntity<List<ThemeResponse>> treeThemes() {
        var result = themeApplicationService.treeThemes();
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZTSJ, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/moveTheme")
    @Operation(summary = "移动主题")
    @LogExecution(operation = EngineOperation.SORT, business = Business.主题域)
//    @Log(
//        business = Business.主题域,
//        operation = Operation.SORT)
    public RestResponseEntity<Boolean> moveTheme(@Valid @RequestBody MoveThemeRequest moveThemeRequest) {
        var result = themeApplicationService.moveTheme(moveThemeRequest);
        return RestResponseEntity.success(result);
    }
}
